package cn.edu.ctbu.gudie.restcontroller;

import cn.edu.ctbu.gudie.domain.Advice;
import cn.edu.ctbu.gudie.domain.Departments;
import cn.edu.ctbu.gudie.domain.Hospitals;
import cn.edu.ctbu.gudie.domain.Patient;
import cn.edu.ctbu.gudie.helper.PageUtils;
import cn.edu.ctbu.gudie.repository.AdviceRepository;
import cn.edu.ctbu.gudie.repository.DepartmentsRepository;
import cn.edu.ctbu.gudie.repository.HospitalsRepository;
import cn.edu.ctbu.gudie.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/webapi/patient")
@Slf4j
public class PatientRestController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private DepartmentsRepository departmentsRepository;
    @Autowired
    private HospitalsRepository hospitalsRepository;
    @Autowired
    private AdviceRepository adviceRepository;

    /**
     * 查询全部患者信息
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<?> FindAll(@RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "1") Integer pageNum) {
        // 校验pageNum是否小于1
        if (pageNum < 1) {
            return ResponseEntity.badRequest().body("Page number must be greater than or equal to 1");
        }
        // 校验pageSize是否小于1
        if (pageSize < 1) {
            return ResponseEntity.badRequest().body("Page size must be greater than or equal to 1");
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Patient> tempPatients = patientService.findAll(pageable);
        PageUtils pageUtils = new PageUtils(tempPatients.getTotalElements(), tempPatients.getContent());
        return ResponseEntity.ok(pageUtils);
    }
    /**
     * 查询患者信息
     *
     * @param patientName 查询关键词
     * @return ResponseEntity 包含查询结果或错误状态码
     */
    @GetMapping("/findByName")
    public ResponseEntity<?> findByPatientName(@RequestParam String patientName) {
        // 根据姓名查询
        List<Patient> patientsByName = patientService.findByPatientName(patientName);
        if (!patientsByName.isEmpty()) {
            return ResponseEntity.ok(patientsByName);
        } else {
            return ResponseEntity.notFound().build(); // 返回404状态码表示未找到
        }
    }
    @PostMapping("/recommend")
    public Map<String, Object> search(@RequestBody List<Integer> tagIds) {
        return patientService.recommendDepartmentsAndHospitals(tagIds);
    }
    @GetMapping("/introduction")
    public List<Departments> getDepartments() {
        return departmentsRepository.findAll();
    }
    @GetMapping("/rank")
    public List<Hospitals> getHospitals() {
        return hospitalsRepository.findAll();
    }
    @GetMapping("/advice")
    public List<Advice> getFeedbacks() {
        return adviceRepository.findAll();
    }
    @PostMapping("/advice")
    public void addFeedback(@RequestBody Advice feedback) {
        patientService.saveAdvice(feedback);
    }

    @DeleteMapping("/advice/{id}")
    public void deleteFeedback(@PathVariable Integer id) {
        patientService.deleteAdvice(id);
    }
}
